package models

import (
	//"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"lottery/libs/mymgo"
	"time"
)

const (
	POLICYDATA_COL_STR = "policydata"
)

type PolicyData struct {
	Id      int64   `json:"id" bson:"_id"`
	UserId  int64   `json:"userId" bson:"userId"`
	MinRate float64 `json:"minRate" bson:"minRate"` // 最低水位
	MaxRate float64 `json:"maxRate" bson:"maxRate"` // 最大水位

	Price int `json:"price" bson:"price"` // 下单金额

	IsDeleted   bool  `json:"isDeleted" bson:"isDeleted"`
	CreateTime  int64 `json:"createTime" bson:"createTime"`
	UpdateTime  int64 `json:"updateTime" bson:"updateTime"`
	DeletedTime int64 `json:"deletedTime" bson:"deletedTime"`
}

func SavePolicyData(policydata *PolicyData) (err error) {

	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}
	policydata.Price = policydata.Price * 100
	policydata.Id = int64(sess.AutoIncId(POLICYDATA_COL_STR))
	policydata.CreateTime = time.Now().UnixNano() / int64(time.Millisecond)
	return sess.Insert(POLICYDATA_COL_STR, policydata)
}

func UpdatePolicData(policydata *PolicyData) (err error) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}
	return sess.Update(POLICYDATA_COL_STR, bson.M{"userId": policydata.UserId, "_id": policydata.Id}, &policydata)
}

func ExistsUserId(userId int64) (exist bool) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}
	return sess.Exist(POLICYDATA_COL_STR, bson.M{"userId": userId})
}

func GetPolicyData(userId int64) (policydata *PolicyData, err error) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}
	policydata = &PolicyData{}
	err = sess.One(POLICYDATA_COL_STR, bson.M{"userId": userId}, policydata)
	return
}
